//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Common Core Extensions
//===============================================================================

using System;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using EntLibContrib.Common;

namespace EntLibContrib.Common.Configuration.Design.Editors
{
	/// <summary>
	/// Provides a user interface for selecting a file name with a specific filter.
	/// </summary>
	/// <remarks>This editor uses a <see cref="FileFilterAttribute"/> to get its file filter from</remarks>
	/// <seealso cref="FileNameEditor"/>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2117:AptcaTypesShouldOnlyExtendAptcaBaseTypes")]
	public class FilteredFileNameEditor : FileNameEditor
  {
		#region Fields
		private FileFilterAttribute fileFilterAttribute;
		private string fileName;
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="FilteredFileNameEditor"/> class.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods")]
		protected FilteredFileNameEditor()
    {
    }
		#endregion

		#region Public Methods
		/// <summary>
		/// Edits the specified object using the editor style provided by the GetEditStyle method.
		/// </summary>
		/// <param name="context">An <para>ITypeDescriptorContext</para> that can be used to gain additional context information.</param>
		/// <param name="provider">A service provider object through which editing services may be obtained.</param>
		/// <param name="value">An instance of the value being edited.</param>
		/// <returns>
		/// The new value of the object. If the value of the object hasn't changed, this should return the same object it was passed.
		/// </returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods")]
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
			fileName = value as string;

			if (context != null && provider != null)
			{
				IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
				if (editorService != null)
				{
					foreach (Attribute attribute in context.PropertyDescriptor.Attributes)
					{
						fileFilterAttribute = attribute as FileFilterAttribute;
						if (fileFilterAttribute != null)
							break;
					}
				}
			}

			return base.EditValue(context, provider, value);
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Initializes the open file dialog when it is created.
		/// </summary>
		/// <param name="openFileDialog">The <see cref="OpenFileDialog"/> to use to select a file name.</param>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2116:AptcaMethodsShouldOnlyCallAptcaMethods")]
		protected override void InitializeDialog(OpenFileDialog openFileDialog)
    {
      base.InitializeDialog(openFileDialog);
			if (fileFilterAttribute != null)
			{
				openFileDialog.Filter = fileFilterAttribute.Filter;
				if (!String.IsNullOrEmpty(fileName))
					openFileDialog.InitialDirectory = Path.GetDirectoryName(Path.GetFullPath(fileName));
			}
    }
		#endregion
	}
}